package com.kou.controller;/**
 * @author SteinY
 * @create 2022-03-28 10:05
 * @description:
 */

import com.alibaba.fastjson.JSONObject;
import com.kou.pojo.Type;
import com.kou.pojo.User;
import com.kou.service.GoodsTypeService;
import com.kou.service.TypeService;
import com.kou.service.impl.AdminServiceImple;
import com.kou.service.impl.GoodsTypeServiceImpl;
import com.kou.service.impl.TypeServiceImpl;
import com.kou.utils.StrUtils;
import com.qf.service.AdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * projectName:myshop2
 * author:SteinY
 * time:2022/3/28 10:05
 * description:
 */
@WebServlet("/searchType")
public class SearchTypesServlet extends HttpServlet {
    TypeService typeService = new TypeServiceImpl();
    GoodsTypeService goodsTypeService = new GoodsTypeServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String level = request.getParameter("level");
        String goodsname = request.getParameter("goodsname");
        List<Type> types= typeService.select(level,goodsname);
        for (Type type : types) {
            String parentName = goodsTypeService.findParentName(type.getParent());
            if (!StrUtils.empty(parentName)){
                parentName = "无";
            }
            type.setParentName(parentName);
        }
        String typeJson = JSONObject.toJSONString(types);
        response.getWriter().write(typeJson);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}